#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
# @Time      :2021/6/1
# @Author    :zhangxuchuo
# @Email     :zhangxuchuo@bbdservice.com
# @File      :EC10_UpaidLoanBal_2D
# @Software  :PyCharm

'''eof
name:企业当前未结清贷款余额
code:EC10_UpaidLoanBal_2D
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''

import json
import sys
import re
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--', 'None']

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def nullCheck(str):
    if str not in null_type_list:
        return True
    else:
        return False

def EC10_UpaidLoanBal_2D():
    factor = []
    data = EC_DSJ_2D.get('data')
    ## 借贷交易汇总信息 EBB [1..1]
    EBB = getTable(data, 'EBB', 1, 0)
    if EBB == [{}]:
        return 0
    elif EBB == 0:
        return 0
    else:
        ## 借贷交易汇总信息单元 EB02 [0..1]
        EB02 = getTable(EBB[0], 'EB02', 0, 1)
        if EB02 ==[{}]:
            return 0
        elif EB02 == 0:
            return 0
        else:
            ## 未结清借贷交易汇总信息段 EB02A [1..1]
            EB02A = getTable(EB02[0], 'EB02A', 1, 0)
            if EB02A == [{}]:
                return 0
            elif EB02A == 0:
                return 0
            else:
                ## 其他交易借贷分类汇总信息 EB02AH [0..20]
                EB02AH = getTable(EB02A[0], 'EB02AH', 0, 1)
                if EB02AH == [{}]:
                    return 0
                elif EB02AH == 0:
                    return 0
                else:
                    for i in EB02AH:
                        ## 业务类型 EB02AD01 [1..1]
                        EB02AD01 = i.get('EB02AD01')
                        ## 余额 EB02AJ06 [1..1]
                        EB02AJ06 = i.get('EB02AJ06')
                        if nullCheck(EB02AD01) and nullCheck(EB02AJ06):
                            try:
                                if EB02AD01 in ['1', '2', '3'] and float(EB02AJ06) >= 0:
                                    factor.append(float(EB02AJ06))
                            except:
                                pass



    factor = sum(factor)

    return factor


# if __name__ == '__main__':

    # import json
    # file_obj = open(r'C:\Users\john\Desktop\samples\EC_DSJ_2D.json', 'r')
    # content = file_obj.read()
    # EC_DSJ_2D = json.loads(content, strict=False)
    # aa = EC10_UpaidLoanBal_2D()
    # print(aa)

result = EC10_UpaidLoanBal_2D()